import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate'
// 渲染进程入口文件
import { createApp } from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
import App from './App.vue'
import { routes } from './router/index'
import { useAppStore } from './stores/app'
import 'element-plus/dist/index.css'

import 'element-plus/theme-chalk/dark/css-vars.css'
import 'uno.css'
import './style.css'

// 创建 Vue 应用
const app = createApp(App)

// 创建路由
const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 创建状态管理
const pinia = createPinia()
pinia.use(createPersistedState())

// 安装插件
app.use(router)
app.use(pinia)
app.use(ElementPlus, { locale: zhCn })

// 启动前预加载全局配置，再挂载应用
async function bootstrap() {
  try {
    const store = useAppStore()
    await store.loadConfig()
  }
  catch (err) {
    console.error('初始化配置失败:', err)
  }

  app.mount('#app')

  // 开发模式下的调试信息
  if (import.meta.env.DEV) {
    console.log('🚀 渲染进程已启动')
    console.log('📦 Vue 版本:', app.version)
    console.log('🔧 开发模式已启用')

    // 暴露全局调试对象
    ;(window as any).__APP__ = app
    ;(window as any).__ROUTER__ = router
    ;(window as any).__PINIA__ = pinia
  }
}
void bootstrap()
